package com.qq.BFMRSE.Module;

import java.io.File;
import java.util.List;

import com.qq.BFMRSE.Hdfs.hdfsOper;
import com.qq.BFMRSE.controllerHelper.updateHelper;
import com.qq.BFMRSE.model.BloomFilter;
import com.qq.BFMRSE.model.indexTree;
import com.qq.BFMRSE.model.indexTreeNode;
import com.qq.BFMRSE.util.BFConstants;

public class updateTool {
	private String userName;
	private List<Integer> pathList;
	private indexTreeNode newNode;
	private indexTreeNode root;
	public indexTreeNode getRoot() {
		return root;
	}
	public void setRoot(indexTreeNode root) {
		this.root = root;
	}
	public List<Integer> getPathList() {
		return pathList;
	}
	public void setPathList(List<Integer> pathList) {
		this.pathList = pathList;
	}
	public indexTreeNode getNewNode() {
		return newNode;
	}
	public void setNewNode(indexTreeNode newNode) {
		this.newNode = newNode;
	}
	
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	
	public updateTool(String userName,List<Integer>pathList,indexTreeNode root,indexTreeNode newNode){
		this.userName=userName;
		this.pathList=pathList;
		this.newNode=newNode;
		this.root=root;
	}
	public updateTool(){}
	public void deUpdateIndexTree()
	{
		indexTreeNode deleteNode=root;
		indexTreeNode broNode=null;
		indexTreeNode currNode=null;
		if(deleteNode!=null)
		{
			for(int i=pathList.size()-1;i>=0;i--)
			{
				if(pathList.get(i)==0)
				{
					deleteNode=deleteNode.getLeft();
				}
				else {
					deleteNode=deleteNode.getRight();
				}
			}
			deleteNode.setBf(new BloomFilter());
			deleteNode.setFID("");
			currNode=deleteNode;
			while(currNode.getParent()!=null)
			{
				if(currNode==currNode.getParent().getLeft())
				{
					broNode=currNode.getParent().getRight();
				}
				else {
					broNode=currNode.getParent().getLeft();
				}
				BloomFilter bf=new BloomFilter();
				int length=bf.BitsLength();
				for(int i=0;i<length;i++)
				{
					if(currNode.getBf().getBits().get(i)==true||broNode.getBf().getBits().get(i)==true)
					{
						bf.getBits().set(i);
					}
				}
				currNode.getParent().setBf(bf);
				currNode=currNode.getParent();
			}
		}
	}
	public void inUpdateIndexTree(List<Integer>newNodeBits)
	{
		indexTreeNode changedNode=root;
		indexTreeNode newFileNode=null;
		indexTreeNode currNode=null;
		indexTreeNode broNode=null;
		if(newNode.getLeft()!=null)
		{
			newNode.getLeft().setParent(newNode);
		}
		if(newNode.getRight()!=null)
		{
			newNode.getRight().setParent(newNode);
		}
		for(int i=pathList.size()-1;i>=0;i--)
		{
			if(pathList.get(i)==0)
			{
				changedNode=changedNode.getLeft();
			}
			else {
				changedNode=changedNode.getRight();
			}
		}
		newFileNode=newNode.getRight();
		newNode.setLeft(changedNode);
	    updateHelper.updateNewNode(newFileNode, newNodeBits);
		if(changedNode.getParent().getLeft()==changedNode)
		{
			changedNode.getParent().setLeft(newNode);
		}
		else {
			changedNode.getParent().setRight(newNode);
		}
		newNode.setParent(changedNode.getParent());
		currNode=newFileNode;
		while(currNode.getParent()!=null)
		{
			if(currNode.getParent().getLeft().equals(currNode))
			{
				broNode=currNode.getParent().getRight();
			}
			else {
				broNode=currNode.getParent().getLeft();
			}
			BloomFilter bf=new BloomFilter();
			int length=bf.BitsLength();
			for(int i=0;i<length;i++)
			{
				if(currNode.getBf().getBits().get(i)==true||broNode.getBf().getBits().get(i)==true)
				{
					bf.getBits().set(i);
				}
			}
			currNode.getParent().setBf(bf);
			currNode=currNode.getParent();
		}
	}
	public void updateIndexTreeEmptyNode(List<Integer>newBitSetList,String fileName)
    {
    	indexTreeNode emptyNode=root;
    	indexTreeNode currNode=null;
    	indexTreeNode broNode=null;
    	for(int i=pathList.size()-1;i>=0;i--)
    	{
    		if(pathList.get(i)==0)
    		{
    			emptyNode=emptyNode.getLeft();
    		}
    		else {
				emptyNode=emptyNode.getRight();
			}
    	}
    	emptyNode.setFID(fileName);
    	updateHelper.updateNewNode(emptyNode, newBitSetList);
    	currNode=emptyNode;
    	while(currNode.getParent()!=null)
    	{
    		if(currNode.getParent().getLeft().equals(currNode))
			{
				broNode=currNode.getParent().getRight();
			}
			else {
				broNode=currNode.getParent().getLeft();
			}
			BloomFilter bf=new BloomFilter();
			int length=bf.BitsLength();
			for(int i=0;i<length;i++)
			{
				if(currNode.getBf().getBits().get(i)==true||broNode.getBf().getBits().get(i)==true)
				{
					bf.getBits().set(i);
				}
			}
			currNode.getParent().setBf(bf);
			currNode=currNode.getParent();
    	}
    }
	

}
